package version1;

import java.awt.BorderLayout;
import java.awt.Checkbox;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.List;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class FiltreringGUI extends JFrame implements ActionListener{
	
	private JButton confirmButton = new JButton("Confirm");
	private ArrayList<JCheckBox> checkboxes = new ArrayList<JCheckBox>(); 
	private ArrayList<String> commands = new ArrayList<String>();
	private CoreTable coretable;
//CoreTableGUI as parameter because, need to update it in actionperformed
	public FiltreringGUI(CoreTable coretable) {
		setLayout(new BorderLayout());
		this.coretable = coretable;
		commands.addAll(CompleteTableNames.getStandardCoreList());
		
        JPanel checkPanel = new JPanel(new GridLayout(0, 1));
        
		//Adding all checkboxes to checkPanel
        for(int i = 0; i < commands.size(); i++){
			
			checkboxes.add(new JCheckBox(""+commands.get(i)));
			add(checkboxes.get(i));
			checkboxes.get(i).addActionListener(this);
			checkPanel.add(checkboxes.get(i));
		}
		
		add(checkPanel, BorderLayout.LINE_START);
		checkPanel.add(confirmButton);
		
		//Center of screen
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		this.setLocation(dim.width/2-this.getSize().width/2, dim.height/2-this.getSize().height/2);
		
		confirmButton.addActionListener(this);
		pack();
		setVisible(true);
		
	
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		ArrayList<String> list = new ArrayList<String>();
		if(e.getSource() == confirmButton){
			for(int i = 0; i < checkboxes.size(); i++){
				if(checkboxes.get(i).isSelected() == true){
					System.out.println(commands.get(i)+" is selected!");
					//TODO Send to condor
					list.add(commands.get(i));
				}
			}
			System.out.println("Inside Filt: "+list);
			coretable.createNewTable(list);

		}
		
	}
}
